<template>
    <div class="fillContainer">
        <div>
            <el-form
                    :inline="true"
                    ref="search_data"
            >
                <el-form-item label="">
                    <el-date-picker
                            v-model="search_data.startTime"
                            type="datetime"
                            placeholder="选择开始时间">
                    </el-date-picker>
                    --
                    <el-date-picker
                            v-model="search_data.endTime"
                            type="datetime"
                            placeholder="选择结束时间">
                    </el-date-picker>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" size="small" icon="search" @click='searchData()'>查询</el-button>
                </el-form-item>

                <el-form-item class="btnRight">
                    <el-button type="primary" size="small" icon="view" @click='add()'>添加</el-button>
                </el-form-item>
            </el-form>
        </div>

        <el-table
                :data="tableData"
                style="width: 100%" border>
            <el-table-column
                    type="index"
                    align="center"
                    label="序号"
                    width="70px">
            </el-table-column>
            <el-table-column
                    label="时间"
                    prop="date"
                    align="center">
                <template slot-scope="scope">
                    <i class="el-icon-time"></i>
                    <span style="margin-left: 10px">{{ scope.row.date }}</span>
                </template>
            </el-table-column>
            <el-table-column
                    label="姓名"
                    prop="name"
                    align="center">
            </el-table-column>
            <el-table-column align="center" label="操作" prop="operation">
                <template slot-scope="scope">
                    <el-button
                            type="warning"
                            size="small"
                            icon="edit"
                            @click="handleEdit(scope.$index, scope.row)">编辑
                    </el-button>
                    <el-button
                            size="small"
                            type="danger"
                            icon="delete"
                            @click="handleDelete(scope.$index, scope.row)">删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>

        <Pagination :paginations="paginations" @updatePageSize="updatePageSize"
                    @updatePageNo="updatePageNo"></Pagination>

        <Dialog :dialog="dialog" @update="getData" :fromData="fromData"></Dialog>
    </div>
</template>

<script>
    import ElTableBody from "element-ui";
    import Dialog from '@/components/admin/CompanyDialog';
    import Pagination from '@/components/Pagination';

    export default {
        name: "Company",
        components: {ElTableBody, Dialog, Pagination},
        data() {
            return {
                tableData: [{
                    id: 11,
                    date: '2016-05-02',
                    name: '王小虎',
                    address: '上海市普陀区金沙江路 1518 弄'
                }, {
                    id: 12,
                    date: '2016-05-04',
                    name: '王小虎',
                    address: '上海市普陀区金沙江路 1517 弄'
                }, {
                    id: 13,
                    date: '2016-05-01',
                    name: '王小虎',
                    address: '上海市普陀区金沙江路 1519 弄'
                }, {
                    id: 14,
                    date: '2016-05-03',
                    name: '王小虎',
                    address: '上海市普陀区金沙江路 1516 弄'
                }],
                fromData: {
                    name: '',
                    remark: '',
                },
                dialog: {
                    show: false,
                    title: '',   //标题
                    option: 'edit',  //操作
                },
                //需要给分页组件传的信息
                paginations: {
                    page_index: 1, // 当前位于哪页
                    total: 0, // 总数
                    page_size: 20, // 1页显示多少条
                    page_sizes: [20, 30, 40, 50], //每页显示多少条
                    layout: "total, sizes, prev, pager, next, jumper" // 翻页属性
                },
                search_data: {
                    startTime: "",
                    endTime: ""
                },
            }
        },
        created() {
            this.getData(this.paginations.page_index, this.paginations.page_size);
        },
        methods: {
            handleEdit(index, row) {
                this.dialog = {
                    show: true,
                    title: "修改用户信息",
                    option: "edit"
                };
                this.fromData = {
                    name: row.name,
                    remark: row.remark,
                    id: row.id
                };
                console.log(index, row);
            }
            ,
            handleDelete(index, row) {
                console.log(row.id);
                console.log(index, row);
            },
            getData() {
                let pageIndex = this.paginations.page_index;
                let pageSize = this.paginations.page_size;
                this.$axios.get("/api/company/pages?pageNo=" + pageIndex + "&pageSize=" + pageSize).then(res => {
                    // 登录成功
                    console.log(res.data);
                    this.paginations.total = res.data.total;

                });
            },
            add() {
                this.dialog = {
                    show: true,
                    title: "添加用户",
                    option: "add"
                };
                this.fromData = {
                    name: '',
                    remark: '',
                    id: ''
                };
            },
            searchData() {
                // 筛选
                if (!this.search_data.startTime || !this.search_data.endTime) {
                    this.$message({
                        type: "warning",
                        message: "请选择时间区间"
                    });
                    return;
                }
                const startTime = this.search_data.startTime.getTime();
                const endTime = this.search_data.endTime.getTime();
                console.log(startTime);
                console.log(endTime);
            },

            updatePageSize(pageSize) {
                this.paginations.page_size = pageSize;
                this.getData();
            },
            updatePageNo(pageNo) {
                this.paginations.page_index = pageNo;
                this.getData();
            },
        },
    }
</script>

<style scoped>
    .fillContainer {
        width: 100%;
        height: 100%;
        padding: 16px;
        box-sizing: border-box;
    }

    .btnRight {
        float: right;
    }

</style>
